<template>
  <div class="app-container">
    <el-form class="formList" :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-row>
        <el-col :span="8">
          <el-form-item label="供应商名称" prop="supplierName">
            <el-input v-model="queryParams.supplierName" placeholder="请输入供应商名称" clearable size="small"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="客户名称" prop="customerName">
            <el-input v-model="queryParams.customerName" placeholder="请输入客户名称" clearable size="small"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="订单号" prop="orderNo">
            <el-input v-model="queryParams.orderNo" placeholder="请输入订单号" clearable size="small"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="用户申请时间" prop="applyTime">
            <el-date-picker clearable size="small" v-model="queryParams.applyTime" type="date" value-format="yyyy-MM-dd"
              placeholder="选择用户申请时间">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="状态" prop="status">
            <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
              <el-option v-for="dict in statusOptions" :key="dict.dictValue" :label="dict.dictLabel"
                :value="dict.dictValue" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="商家退款时间" prop="confirmTime">
            <el-date-picker clearable size="small" v-model="queryParams.confirmTime" type="date"
              value-format="yyyy-MM-dd" placeholder="选择商家退款时间">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="退款状态" prop="backStatus">
            <el-select v-model="queryParams.backStatus" placeholder="请选择退款状态" clearable size="small">
              <el-option v-for="dict in backStatusOptions" :key="dict.dictValue" :label="dict.dictLabel"
                :value="dict.dictValue" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="微信退款单号" prop="refundId">
            <el-input v-model="queryParams.refundId" placeholder="请输入微信退款单号" clearable size="small"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="退款渠道" prop="backType">
            <el-select v-model="queryParams.backType" placeholder="请选择退款渠道" clearable size="small">
              <el-option v-for="dict in backTypeOptions" :key="dict.dictValue" :label="dict.dictLabel"
                :value="dict.dictValue" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-form-item style="width: 100%;display: flex; align-items: center; justify-content: center;">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="row-mbinfo">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['order:back:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['order:back:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['order:back:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['order:back:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <div class="divTable">
      <el-table v-loading="loading" :data="backList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="ID" align="center" prop="backUid" sortable />
        <el-table-column label="供应商名称" align="center" prop="supplierName" :show-overflow-tooltip="true" />
        <el-table-column label="客户名称" align="center" prop="customerName" :show-overflow-tooltip="true" />
        <el-table-column label="订单号" align="center" prop="orderNo" :show-overflow-tooltip="true" />
        <el-table-column label="订单支付金额" align="center" prop="payAmount" :show-overflow-tooltip="true" />
        <el-table-column label="退款金额" align="center" prop="backAmount" :show-overflow-tooltip="true" />
        <el-table-column label="申请原因" align="center" prop="applyReason" :show-overflow-tooltip="true" />
        <el-table-column label="用户申请时间" align="center" prop="applyTime" width="180" sortable>
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.applyTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" />
        <el-table-column label="退款原因" align="center" prop="backReason" :show-overflow-tooltip="true" />
        <el-table-column label="商家退款时间" align="center" prop="confirmTime" width="180" sortable>
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.confirmTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="商家审核意见" align="center" prop="auditRemark" :show-overflow-tooltip="true" />
        <el-table-column label="退款状态" align="center" prop="backStatus" :formatter="backStatusFormat" />
        <el-table-column label="微信退款单号" align="center" prop="refundId" :show-overflow-tooltip="true" />
        <el-table-column label="退款渠道" align="center" prop="backType" :formatter="backTypeFormat" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-zoom-in" @click="handleShowDetail(scope.row)"
              v-hasPermi="['order:back:query']">查看</el-button>
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['order:back:edit']">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
              v-hasPermi="['order:back:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>
    
    <!-- 添加或修改用户退款信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body @keyup.enter.native="submitForm">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="商家ID" prop="supplierUid">
          <el-input v-model="form.supplierUid" placeholder="请输入商家ID" />
        </el-form-item>
        <el-form-item label="供应商名称" prop="supplierName">
          <el-input v-model="form.supplierName" placeholder="请输入供应商名称" />
        </el-form-item>
        <el-form-item label="客户UID" prop="customerUid">
          <el-input v-model="form.customerUid" placeholder="请输入客户UID" />
        </el-form-item>
        <el-form-item label="客户名称" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入客户名称" />
        </el-form-item>
        <el-form-item label="订单ID" prop="orderUid">
          <el-input v-model="form.orderUid" placeholder="请输入订单ID" />
        </el-form-item>
        <el-form-item label="订单号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="订单明细ID" prop="itemUid">
          <el-input v-model="form.itemUid" placeholder="请输入订单明细ID" />
        </el-form-item>
        <el-form-item label="订单支付金额" prop="payAmount">
          <el-input v-model="form.payAmount" placeholder="请输入订单支付金额" />
        </el-form-item>
        <el-form-item label="退款金额" prop="backAmount">
          <el-input v-model="form.backAmount" placeholder="请输入退款金额" />
        </el-form-item>
        <el-form-item label="申请原因" prop="applyReason">
          <el-input v-model="form.applyReason" placeholder="请输入申请原因" />
        </el-form-item>
        <el-form-item label="用户申请时间" prop="applyTime">
          <el-date-picker clearable size="small" v-model="form.applyTime" type="date" value-format="yyyy-MM-dd"
            placeholder="选择用户申请时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option v-for="dict in statusOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="退款原因" prop="backReason">
          <el-input v-model="form.backReason" placeholder="请输入退款原因" />
        </el-form-item>
        <el-form-item label="商家退款时间" prop="confirmTime">
          <el-date-picker clearable size="small" v-model="form.confirmTime" type="date" value-format="yyyy-MM-dd"
            placeholder="选择商家退款时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="商家审核意见" prop="auditRemark">
          <el-input v-model="form.auditRemark" placeholder="请输入商家审核意见" />
        </el-form-item>
        <el-form-item label="退款状态" prop="backStatus">
          <el-select v-model="form.backStatus" placeholder="请选择退款状态">
            <el-option v-for="dict in backStatusOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="微信退款单号" prop="refundId">
          <el-input v-model="form.refundId" placeholder="请输入微信退款单号" />
        </el-form-item>
        <el-form-item label="退款渠道" prop="backType">
          <el-select v-model="form.backType" placeholder="请选择退款渠道">
            <el-option v-for="dict in backTypeOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看用户退款信息对话框 -->
    <el-dialog :title="viewTitle" :visible.sync="viewOpen" width="700px" append-to-body>
      <el-form ref="detailForm" label-width="100px">
        <el-row>
          <el-col :span="12" v-for="(item, index) in showData" :key="index" style="margin-top:-15px">
            <el-form-item :label="`${item.label}：`">
              <span>{{item.value}}</span>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="mini" @click="handCloseDetail">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listBack,
    getBack,
    delBack,
    addBack,
    updateBack
  } from "@/api/order/back";

  export default {
    name: "Back",
    components: {},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 用户退款信息表格数据
        backList: [],
        // 弹出层标题
        title: "",
        //查看详细标题
        viewTitle: "",
        // 是否显示弹出层
        open: false,
        //是否显示查看详细弹出层
        viewOpen: false,
        //查看详细数据
        showData: [],
        // 状态字典
        statusOptions: [],
        // 退款状态字典
        backStatusOptions: [],
        // 退款渠道字典
        backTypeOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          supplierName: null,
          customerName: null,
          orderNo: null,
          applyTime: null,
          status: null,
          confirmTime: null,
          backStatus: null,
          refundId: null,
          backType: null
        },
        columns: [{
            label: 'ID',
            dataIndex: 'backUid',
          },
          {
            label: '商家ID',
            dataIndex: 'supplierUid',
          },
          {
            label: '供应商名称',
            dataIndex: 'supplierName',
          },
          {
            label: '客户UID',
            dataIndex: 'customerUid',
          },
          {
            label: '客户名称',
            dataIndex: 'customerName',
          },
          {
            label: '订单ID',
            dataIndex: 'orderUid',
          },
          {
            label: '订单号',
            dataIndex: 'orderNo',
          },
          {
            label: '订单明细ID',
            dataIndex: 'itemUid',
          },
          {
            label: '订单支付金额',
            dataIndex: 'payAmount',
          },
          {
            label: '退款金额',
            dataIndex: 'backAmount',
          },
          {
            label: '申请原因',
            dataIndex: 'applyReason',
          },
          {
            label: '用户申请时间',
            dataIndex: 'applyTime',
          },
          {
            label: '状态',
            dataIndex: 'status',
          },
          {
            label: '退款原因',
            dataIndex: 'backReason',
          },
          {
            label: '商家退款时间',
            dataIndex: 'confirmTime',
          },
          {
            label: '商家审核意见',
            dataIndex: 'auditRemark',
          },
          {
            label: '退款状态',
            dataIndex: 'backStatus',
          },
          {
            label: '微信退款单号',
            dataIndex: 'refundId',
          },
          {
            label: '退款渠道',
            dataIndex: 'backType',
          },
        ],
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.getList();
      this.getDicts("sys_data_status").then(response => {
        this.statusOptions = response.data;
      });
      this.getDicts("order_back_status").then(response => {
        this.backStatusOptions = response.data;
      });
      this.getDicts("order_back_type").then(response => {
        this.backTypeOptions = response.data;
      });
    },
    methods: {
      /** 查询用户退款信息列表 */
      getList() {
        this.loading = true;
        listBack(this.queryParams).then(response => {
          this.backList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 状态字典翻译
      statusFormat(row, column) {
        return this.selectDictLabel(this.statusOptions, row.status);
      },
      // 退款状态字典翻译
      backStatusFormat(row, column) {
        return this.selectDictLabel(this.backStatusOptions, row.backStatus);
      },
      // 退款渠道字典翻译
      backTypeFormat(row, column) {
        return this.selectDictLabel(this.backTypeOptions, row.backType);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          backUid: null,
          supplierUid: null,
          supplierName: null,
          customerUid: null,
          customerName: null,
          orderUid: null,
          orderNo: null,
          itemUid: null,
          payAmount: null,
          backAmount: null,
          applyReason: null,
          applyTime: null,
          status: null,
          backReason: null,
          confirmTime: null,
          auditRemark: null,
          backStatus: null,
          refundId: null,
          backType: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.backUid)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加用户退款信息";
      },
      /** 查看详情按钮操作 **/
      handleShowDetail(row) {
        this.viewOpen = true;
        this.viewTitle = "查看用户退款信息";
        this.showData = [];
        this.columns.forEach(item => {
          this.showData.push({
            label: item.label,
            value: row[item.dataIndex]
          })
        })
      },
      handCloseDetail() {
        this.viewOpen = false
        this.showData = []
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const backUid = row.backUid || this.ids
        getBack(backUid).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改用户退款信息";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.backUid != null) {
              updateBack(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addBack(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const backUids = row.backUid || this.ids;
        this.$confirm('是否确认删除用户退款信息编号为"' + backUids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delBack(backUids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('order/back/export', {
          ...this.queryParams
        }, `order_back.xlsx`)
      }
    }
  };
</script>
